﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Montezuma
{
    class Lanca
    {
        Texture2D textura;
        public Vector2 posicao;
        public Rectangle retanguloPonta;
        public Rectangle retanguloCabo;
        public bool mover = false;
        public bool inverter;
        SpriteEffects spriteEffects = SpriteEffects.None;
        public int posicaoInicial;
        public int posicaoFinal;
        public int direcao = 1;
        int velocidade;

        public TimeSpan tempoLanca = TimeSpan.Zero;
        public float tempoespera = 2;

        public Lanca(Texture2D textura, Vector2 posicao, bool inverter, int posicaoInicial, int posicaoFinal, int velocidade)
            : this(textura, posicao, inverter)
        {
            this.posicaoInicial = posicaoInicial;
            this.posicaoFinal = posicaoFinal;
            this.velocidade = velocidade;
            
            mover = true;
        }

        public Lanca(Texture2D textura, Vector2 posicao, bool inverter)
        {
            this.textura = textura;
            this.posicao = posicao;
            this.inverter = inverter;
            retanguloPonta = new Rectangle((int)posicao.X + 5, (int)posicao.Y + 5, 22, 15);
            retanguloCabo = new Rectangle((int)posicao.X + 27, (int)posicao.Y + 10, 142, 4);

            if (inverter)
            {
                spriteEffects = SpriteEffects.FlipHorizontally;
                retanguloPonta = new Rectangle((int)posicao.X + textura.Width - 27, (int)posicao.Y + 5, 22, 15);
            }
        }

        public void Update(GameTime gameTime)
        {
            if (mover)
            {
                retanguloPonta = new Rectangle((int)posicao.X, (int)posicao.Y, 30, textura.Height);
                retanguloCabo = new Rectangle((int)posicao.X + 27, (int)posicao.Y + 10, 142, 4);

                if (tempoLanca.TotalSeconds <= 0)
                {
                    posicao.X += direcao * velocidade;
                }

                if (posicao.X < posicaoInicial)
                {
                    direcao = 1;
                }

                if (posicao.X > posicaoFinal)
                {
                    direcao = -1;
                    tempoLanca += gameTime.ElapsedGameTime;

                    if (tempoLanca.TotalSeconds > tempoespera)
                    {
                        tempoLanca = TimeSpan.Zero;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textura, posicao, new Rectangle(0, 0, textura.Width, textura.Height), Color.White, 0, Vector2.Zero, 1, spriteEffects, 0);
        }
    }
}
